//
//  LoopNode.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/23.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation

import Eelay
import AsyncDisplayKit


open class EmptyNode: JoCellNode {
    let line = ASDisplayNode()
    var height:CGFloat = 10
    open override func addLayoutRules() {
        backgroundColor = .clear
    }
  
    
    open override func specGetter() -> _Spec {
        return line["\(0)"]
    }
    
}


open class BlankNode: JoCellNode {
    let line = ASDisplayNode()
    var height:CGFloat = 10
    open override func addLayoutRules() {
//        super.addLayoutRules()
        backgroundColor = .clear
    }
    open override func mo_model_reload() {
        if let hex = mo.model["hex"] as? String{
            line.view.backgroundColor = UIColor(shex: mo.model["hex",UIColor.table_bg_hex])
        }
        if let color = mo.model["color"] as? UIColor{
            line.view.backgroundColor = color
        }
        
//        separatorHeight = mo.model[float:"height",0]
        height = mo.model[float:"height",10]

        
    }
    open override func specGetter() -> _Spec {
        return line["\(height)"]
    }
    
}


open class BlankCellNode<Line:LineHeight>: JoCellNode {
    let line = ASDisplayNode()
    open override func mo_model_reload() {
        separatorColor = UIColor(shex: mo.model["hex",UIColor.table_bg_hex])
        separatorHeight = Line.height
    }
    open override func specGetter() -> _Spec {
        return _Spec.inset([], .zero, child: line[Swidth,"0"])
    }
    
}

open class LoopKitNode: JoTouchNode {
    
    public static var defaultTapAction:(String,NSMutableDictionary,UIViewController?)->Void = {_,_,_  in }

    open class var width:CGFloat{
        get{
            return Swidth
        }
    }
    open class var height:CGFloat{
        get{
            return 200.co
        }
    }
    open class var pageControllAlignment:HorizontalAlignment{
          get{
            return .center(.zero)
          }
      }
    
    
    
    
    public let imgNode = JoImageNode()
    //            let shadowV = ASDisplayNode()
    //            let textL = TextNode()
    
    public class TextNode:MoDisplayNode{
        let textV = ASTextNode()
        override public func addLayoutRules() {
            textV.maximumNumberOfLines = 1
        }
        override public func specGetter() -> _Spec {
            return .stack([[0,10,0,100],"30","h",&-.center], childs: [
                textV[_shrink(1),_grow(1),Swidth]
            ])
        }
    }
    
    
    
     open override func specGetter() -> _Spec {
        let h =  type(of: self).height
        return _Spec.stack([], childs: [
            imgNode["\(h)",Swidth],
            ])
    }
    
    open override func mo_model_reload() {
        imgNode.img_url = mo.model["img",""]
    }
}


open class LoopNode<Kit:LoopKitNode>:ASSizeRangeCell,EeActionDelegate{
    
    open func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
//        print("--s-s--s-s-s-00000")
//        if actionID.hasPrefix("tap")
//        {
//        }
//        LoopKitNode.defaultTapAction(actionID,data,mo.controller)

    }

    open lazy var imgsV:JoLoopView<Kit> = JoLoopView<Kit>(Kit.pageControllAlignment,size:CGSize(width: Kit.width, height: Kit.height))
    
    open override func addLayoutRules() {
        
        backgroundColor = ._background
        imgsV.clipsToBounds = false
        imgsV.scrollView.clipsToBounds = false
        imgsV.pageControl.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)

//            CGAffineTransform(translationX: offsetX, y: 0);

//        imgsV.pageControl.setValue(UIImage(named: "page_indicator_white")?.rt_tintedImage(with: UIColor(shex: "#bbb")), forKey: "_pageImage")
//        imgsV.pageControl.setValue(UIImage(named: "page_indicator_white")?.rt_tintedImage(with: UIColor(shex: ___city.hex1)), forKey: "_currentPageImage")
    }
    
    open override func specGetter() -> _Spec {
        return .stack([Kit.width,"\(Kit.height)"], childs: [
            imgsV[Kit.width,"\(Kit.height)"],
        ])
    }
    public var nodePath = "options"
    
    open override func mo_model_reload() {
        imgsV.mo.controller = self.mo.controller
        imgsV.deletate = self
        imgsV.datas = DataNode.path(node: nodePath, mo.model, value: NSMutableArray())
        
        //            mo.model["__load"] = "YES"
    }
    deinit {
    }
}
